//
//  SelectTimePickerView.swift
//  Exchange
//
//  Created by 孟祥群 on 2018/4/23.
//  Copyright © 2018年 common. All rights reserved.
//

import UIKit

class SelectTimePickerView: SelectPickerView, UIPickerViewDataSource, UIPickerViewDelegate {
    var hourData:Array<String> = []
    var minuteData:Array<String> = []
    var hourStr:String = "00"
    var minuteStr:String = "00"
    
    lazy var pickView: UIPickerView = {
        let pickView = UIPickerView()
        pickView.backgroundColor = UIColor.white
        pickView.delegate = self
        pickView.dataSource = self
        return pickView
    }()
    
    override init(style:selectStyle, title:String, selectStr:AnyObject?, dataArr:Array<Any>, dataSource:SlecterProcotol) {
        super.init(style:style, title: title, selectStr:selectStr, dataArr: dataArr, dataSource:dataSource)
        for i in 0...23 {
            if i < 10 {
                hourData.append(String(format: "0%d",i))
            }else {
            hourData.append(String(format: "%d", i))
            }
        }
        for i in 0...59 {
            if i < 10 {
                minuteData.append(String(format: "0%d",i))
            }else {
                minuteData.append(String(format: "%d", i))
            }
        }
        self.setUpUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setUpUI() -> Void {
        self.addSubview(pickView)
        pickView.snp.makeConstraints { (make) in
            make.top.left.right.bottom.equalToSuperview()
        }
        
        self.selectContent = selectContent
        let hourArray = (self.selectContent as! NSString).components(separatedBy: ":")
        if hourArray.count == 2 {
            let hour = hourArray.first
            let minute = hourArray.last
            
            let index = (hourData as NSArray).index(of: hour as Any)
            let minutIndex = (minuteData as NSArray).index(of: minute as Any)
            pickView.selectRow(index, inComponent: 0, animated: false)
            pickView.selectRow(minutIndex, inComponent: 1, animated: false)
        }
    }
    
    func numberOfComponents(in pickerView: UIPickerView) -> Int {
        return 2
    }
    
    func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
        let number =  component == 0 ? 24 : 60
        return number
    }
    
    func pickerView(_ pickerView: UIPickerView, rowHeightForComponent component: Int) -> CGFloat {
        return 44
    }
    
    func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
        if component == 0 {
            let hour = hourData[row]
           return String(format: "%@%@", hour,kText("时"))
        }else {
            let minute:String = minuteData[row]
            return String(format: "%@%@", minute,kText("分"))
            
        }
   
    }
    
    func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
        if component == 0 {
            hourStr = String(format: "%@", hourData[row])
        }
        if component == 1 {
            minuteStr = String(format: "%@", minuteData[row])
        }
        let str = String(format: "%@:%@", hourStr,minuteStr)
        self.selectContent = str as AnyObject
    }
    
    
    /*
    // Only override draw() if you perform custom drawing.
    // An empty implementation adversely affects performance during animation.
    override func draw(_ rect: CGRect) {
        // Drawing code
    }
    */

}
